import {
  ErrorRequestHandler,
  Request,
  Response,
  NextFunction as NF,
  response,
  NextFunction,
} from "express";
import { tokenTime } from "../../config";
import ArticleSer from "../../services/ArticleSer";
import { findtitlelimit, userinfo, type asyncPromise } from "../../interface";
import { routerHandleReturn } from "../../type";
import { info, err } from "../middleware/infoerr";

export default {
  async getTopHeat(req: Request, res: Response): routerHandleReturn {
    const data = await ArticleSer.getTopHeat();
    if (data instanceof Object) return info(res, "ok", data);
    return err(res, "位置错误");
  },
  async getTopTime(req: Request, res: Response): routerHandleReturn {
    const data = await ArticleSer.getTopTime();
    if (data instanceof Object) return info(res, "ok", data);
    return err(res, "位置错误");
  },
  async getTopGift(req: Request, res: Response): routerHandleReturn {
    const data = await ArticleSer.getTopGift();
    if (data instanceof Object) return info(res, "ok", data);
    return err(res, "位置错误");
  },
  async getAlllimt(req: Request, res: Response): routerHandleReturn {
    const data = await ArticleSer.getAlllimit(
      req.query as { x: string; y: string }
    );
    if (data instanceof Object) return info(res, "ok", data);
    return err(res, "获取文章失败");
  },
  async getFindTitle(
    req: Request,
    res: Response,
    next: NextFunction
  ): routerHandleReturn {
    try {
      if (!req.query || !req.query.title || !req.query.x || !req.query.y)
        return err(res, "查询条件错误");
      const findtitles = {
        title: req.query.title as string,
        x: +req.query.x,
        y: +req.query.y,
      };
      const data = await ArticleSer.getFindTitle(findtitles);
      if (data instanceof Object) {
        data;
        let str: string = req.query.title as any;
        // const reg1 = new RegExp(str, "gi");
        const reg2 = new RegExp(str, "g");
        const ddd = (data as any).rows.map((item: any) => {
          if (str.toUpperCase() === item.author.toUpperCase()) {
            item.author = `<font style="color:#994dcd;">${item.author}</font>`;
            return item;
          }
          const title = item.title.replace(
            reg2,
            `<span style="color:red;">${str}</span>`
          );
          const titles = item.titles.replace(
            reg2,
            `<span style="color:red;">${str}</span>`
          );
          const author = item.author.replace(
            reg2,
            `<font style="color:red;">${str}</font>`
          );
          item.title = title;
          item.titles = titles;
          item.author = author;
          return item;
        });
        data.rows = ddd;
        return info(res, "ok", data);
      }
      return err(res, "未知错误");
    } catch (error) {
      next(error);
    }
  },
  async add(req: Request, res: Response, next: NextFunction) {
    try {
      const $ = req.body;
      if (
        !$.author ||
        !$.browesNum ||
        !$.comment ||
        !$.content ||
        !$.createdTime ||
        !$.fontlength ||
        !$.label ||
        !$.title ||
        !$.titles
      )
        return err(res, "参数有误", $);
      const data = await ArticleSer.add($);
      console.log(data);
      if (data) return info(res, "ok", data);
      return err(res, "插入失败");
    } catch (error) {
      next(error);
    }
  },
  async findpk(req: Request, res: Response, next: NextFunction) {
    try {
      if (!req.query.id || Object.is(+req.query.id, NaN)) {
        return err(res, "id错误", { id: req.query.id });
      }
      if (typeof req.query.id === "string") {
        const data = await ArticleSer.findpk(req.query.id);
        if (!data) return err(res, `找不到id为:${req.query.id}的文章`);
        let browesNum: number | string = +(data.browesNum as string);
        console.log("前", browesNum);
        browesNum++;
        console.log("后", browesNum);
        info(res, "ok", data);
        browesNum = String(browesNum);
        const result = await ArticleSer.update(+req.query.id, { browesNum });
        console.log(result);
        return;
      }
      return err(res, "未知错误");
    } catch (error) {
      next(error);
    }
  },

  async Alllabel(req: Request, res: Response) {
    const result = (await ArticleSer.Alllabel()) as unknown as Array<{
      label: string;
    }>;
    const data = result.map((item) => {
      if (item.label) {
        return item.label;
      }
    });
    const set = new Set(data);
    set.delete(undefined);
    return info(res, "ok", [...set]);
  },
};
